package com.anpy.personal;


import com.auth0.jwt.JWT;
import com.auth0.jwt.interfaces.DecodedJWT;

public class TokenUtils {
    public static String getUserIdFromToken(String token) {
        try {
            // Assuming the token is in the format "Bearer <token>"
            String jwtToken = token.substring(7);
            DecodedJWT decodedJWT = JWT.decode(jwtToken);
            return decodedJWT.getClaim("userId").asString();
        } catch (Exception e) {
            throw new RuntimeException("Invalid token", e);
        }
    }

    public static Long parseTokenToUserId(String token) {
        // 解析 token 并提取 userId
        // 假设 token 的格式为 "userId:signature"
        String[] parts = token.split(":");
        if (parts.length == 2) {
            try {
                return Long.parseLong(parts[0]);
            } catch (NumberFormatException e) {
                throw new IllegalArgumentException("Invalid token format");
            }
        } else {
            throw new IllegalArgumentException("Invalid token format");
        }
    }
}
